package com.uyaer.wheel.common
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.SharedObject;

	public class UserData
	{
		private static var m_instance:UserData;
		
		public var maxStarCount:uint = 0;
		
		/**
		 *星星数量 
		 */		
		public var starCount:uint = 0;
		/**
		 *等级 
		 */		
		public var level:uint = 1;
		
		private var shared:SharedObject;
		
		public function UserData()
		{
			shared = SharedObject.getLocal("userdata");
		}
		
		public static function get me():UserData{
			return m_instance ||= new UserData();
		}
		
		public function init():void{
			starCount = 0;
			level = 1;
		}
		
		public function read():void{
			var data:Object = shared.data.userdata;
			if(Config.platform == Platform.MOBILE){
				var file:File = File.userDirectory.resolvePath("com.uyaer.wheel.userdata.db");
				var fileStream:FileStream = new FileStream();
				if(file.exists){
					fileStream.open(file,FileMode.READ);
					data = fileStream.readObject();
					maxStarCount = data.maxStarCount;
				}else{
					maxStarCount = 0;
				}
			}else{
				if(data){
					maxStarCount = data.maxStarCount;
				}else{
					maxStarCount = 0;
				}
			}
		}
		
		public function save():void{
			var data:Object = new Object();
			data.maxStarCount = maxStarCount;
			if(Config.platform == Platform.MOBILE){
				var file:File = File.userDirectory.resolvePath("com.uyaer.wheel.userdata.db");
				var fileStream:FileStream = new FileStream();
				fileStream.open(file,FileMode.WRITE);
				fileStream.writeObject(data);
				fileStream.close();
			}else{
				shared.data.userdata = data;
			}
		}
	}
}